// pages/hotNewsMore/hotNewsMore.js
const app = getApp();
const api = require('../../utils/api')
const allList = require('../../utils/allList.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    level:'',//等级
    areaCode:'',//地区
    type:'',//种类
    nowPage: 1,
    reserve: [],
    isScrollDown: false,//是否到底
    isScrollDownLoding: false,//底部loding
    listLoding: false,//列表loding
    isColumns:false,//城市选择框是否开启
    isRefresher:false,//是否开启下拉
    nowCity: 0,//当前城市
    nowType: 0,//当前类别
    nowLevel: 0//当前等级
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      scale: app.globalData.scale,
      listLoding:true,
      typeList: allList.hotTypeList,
      columns:allList.cityList
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getReserve();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  //滑动到底部触发
  scrollDown() {
    if (this.data.isScrollDown) return;
    this.setData({ isScrollDownLoding: true })
    this.data.nowPage++;
    this.getReserve();
  },


  //点击查询
  onClickSearch(event) {
    this.setData({
      isScrollDown:false,
      nowPage: 1,
      topNub: 0
    })
    this.getReserve();
  },

  //键盘输入时触发
  bindinput(event){
    this.setData({
      value: event.detail.value
    })
  },

  //获取预约预定列表
  getReserve() {
    this.setData({
      listLoding:true
    })
    api.getHotNews({
      "nType":this.data.type,//类别
      "areaCode":this.data.areaCode,//地区
      "keyWord":this.data.value,//关键字
      "currIndex":this.data.nowPage,
      "pageSize":"10"
    }).then((res)=>{
      if(res.data.code==='1') return;
      //console.log(res);
      let time = setTimeout(() => {
        if (res.data.data.length == 0)//如果没有更多数据
          return this.setData({
            isScrollDown: true,
            isScrollDownLoding: false,
            listLoding:false,
            isRefresher:false
          });
        let reserve = this.data.reserve;
        let list = res.data.data;
        list.forEach((i,index)=>{
            list[index].newsType = i.newsType.split(',').splice(0, 5);;
        })
        reserve = this.data.nowPage == 1 ? list : reserve.concat(list);
        this.setData({
          reserve: reserve,
          isScrollDownLoding: false,
          listLoding: false,
          isRefresher:false
        })
        clearTimeout(time);
      }, 500)
      
    })
  },

  //修改城市
  onCityChange(event) {
    const { picker, value, index } = event.detail;
    //console.log(event.detail);
    this.setData({
      isColumns: false,
      isScrollDown: false,
      nowPage: 1,
      reserve: [],
      nowCity: index,
      topNub: 0
    })
    this.data.areaCode = index ? 3300 + index + '00' : '';
    this.getReserve();
    //console.log(this.data.level);
  },

  //打开外部链接
  onClickOutLink(e) {
    console.log(e);
    if (app.openLoginPage()) return;
    app.toOutLink(e);
  },

  //打开修改城市按钮
  onClickCity() {
    this.setData({
      isColumns: true
    })
  },

  //关闭城市按钮
  onClickHide() {
    this.setData({
      isColumns: false
    })
  },

  //点击修改类别
  onClickType(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      nowType: index,
      isScrollDown:false,
      nowPage: 1,
      reserve: [],
      topNub:0,
      type: index ? this.data.typeList[index] : ''
    })
    this.getReserve();
  },

  //下拉刷新
  onRefresher(){
    this.setData({
      isRefresher:true,
      isScrollDown:false,
      nowPage: 1
    })
    this.getReserve();
  },

  //清空输入框
  onClickDelete(){
    this.setData({
      value:''
    })
    this.getReserve();
  }
})